package calcul;

import java.util.ArrayList;

public class BinaryTransform {
	private String normalizeBinarryNum(String binaryNumber){
		int nrZeroBefore = 8 - binaryNumber.length();
		String s = new String() ;
		
		for(int i = 0 ; i < nrZeroBefore ; i++){
			s +="0";
		}
		s += binaryNumber ; 
		return s ;

	}
	public String toBinarry(ArrayList<Integer> arr) throws Exception{
		
		String finalBinaryNr = new String();

		for (Integer integer : arr) {
			String temp = Integer.toBinaryString(integer);
			temp = normalizeBinarryNum(temp);
			finalBinaryNr += temp;
		}
		return finalBinaryNr;
	}
	
	public ArrayList<Integer> toInteger(String binaryNumber) throws Exception{
		ArrayList arr = new ArrayList();
		if(binaryNumber.length() != 32){
			throw new Exception("invalid ip or mask binary form");
		}
		for(int i = 0, temp = 0 ; i < 4 ; i++){
			
			String s = binaryNumber.substring(temp, temp + 8);
			temp +=8;
			arr.add(Integer.parseInt(s, 2));
		}
		return arr;
	}
}
